package com.fhz.mvvmshopjava.ui.goods;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fhz.mvvmshopjava.R;
import com.fhz.mvvmshopjava.adapter.OrderAdapter;
import com.fhz.mvvmshopjava.core.BaseFragment;
import com.fhz.mvvmshopjava.data.model.LoggedInUser;
import com.fhz.mvvmshopjava.databinding.GoodsFragmentBinding;
import com.fhz.mvvmshopjava.entity.Order;
import com.fhz.mvvmshopjava.utils.Constant;
import com.fhz.mvvmshopjava.utils.UserManager;
import com.fhz.utilsfhz.ApiResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class GoodsFragment extends BaseFragment<GoodsViewModel, GoodsFragmentBinding> implements OrderAdapter.OrderClick {

    OrderAdapter orderAdapter;
    List<Order> list;
    int userId;
    @Override
    protected int getLayoutId() {
        return R.layout.goods_fragment;
    }


    @Override
    public void orderAdd(int i) {
        Order order = list.get(i);
        order.setGoodsCount(order.getGoodsCount()+1);
        list.set(i,order);
        mViewModel.updateOrderNum(order.getOrderId(),order.getGoodsCount()).observe(this, listApiResponse -> {
            Toast.makeText(getActivity(), listApiResponse.data, Toast.LENGTH_SHORT).show();
            orderAdapter.notifyDataSetChanged();

        });
    }

    @Override
    public void orderSub(int i) {
        Order order = list.get(i);
        order.setGoodsCount(order.getGoodsCount()-1);
        list.set(i,order);
        mViewModel.updateOrderNum(order.getOrderId(),order.getGoodsCount()).observe(this, listApiResponse -> {
            Toast.makeText(getActivity(), listApiResponse.data, Toast.LENGTH_SHORT).show();
            orderAdapter.notifyDataSetChanged();

        });
    }

    @Override
    protected void initData() {

        mBinding.goodsRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        list = new ArrayList<>();
        orderAdapter = new OrderAdapter(list);
        orderAdapter.setOrderClick(this);
        mBinding.goodsRv.setAdapter(orderAdapter);

        userId = mViewModel.getUserManager().getUserId();
        if (userId != Constant.NO_LOGGED) {
            mViewModel.getAllOrderByUser(userId, Constant.NO_BUY_STATE).observe(this,orderApiResponse -> {
                list.clear();
                list.addAll(orderApiResponse.getData());
                orderAdapter.notifyDataSetChanged();

            });
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
//            Toast.makeText(getActivity(), "g", Toast.LENGTH_SHORT).show();
            if(mViewModel != null){
                UserManager userManager = mViewModel.getUserManager();
                if(userManager != null){
                    int userId = userManager.getUserId();
                    mViewModel.getAllOrderByUser(userId, Constant.NO_BUY_STATE).observe(this,orderApiResponse -> {
                        list.clear();
                        list.addAll(orderApiResponse.getData());
                        orderAdapter.notifyDataSetChanged();
                    });
                }
            }
        }
    }



    @Override
    protected GoodsViewModel createViewModel() {
        return new ViewModelProvider(this).get(GoodsViewModel.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}